<template>
  <div>
    <el-select v-model="emp_code" size="mini" :style="{width:inputStyle?'calc(100% - 20px)':'100%'}" clearable
               @clear="clear" @change="dataChanged" :disabled="disabled"
               filterable remote :remote-method="findOptions" :loading="ei_loading" loading-text="加载中">
      <el-option
          v-for="item in ei_list"
          :key="item.id"
          :label="item.name"
          :value="item.id">
      </el-option>
    </el-select>
    <i class="el-icon-edit el-input__icon" title="来自手动输入"  v-if="inputStyle" style="line-height: normal;width: 20px;"></i>
  </div>
</template>

<script>
export default {
  data() {
    return {
      emp_code:"",
      ei_list:[],
      ei_loading:false,
      inputStyle:false,
    }
  },
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: {
      type: String,
      default: function () {
        return null;
      }
    },
    disabled: {
      type: Boolean,
      default: function () {
        return false;
      }
    },
    inputAdd: {
      type: Boolean,
      default: function () {
        return true;
      }
    },
  },
  watch: {
    value: {
      async handler(newval, oldval) {
        this.inputStyle=false;
        if (newval != null && newval != "") {
          this.emp_code = newval;
          this.ei_list = await elm.post('/business/my/GetEIData', {emp_code: this.emp_code});
          if(this.ei_list.length==0  && this.inputAdd==true)
          {
            this.inputStyle=true;
            this.ei_list.push({id:name,name:name});
          }
        }
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    dataChanged(){
      this.$emit('update:value', this.emp_code);
      this.$emit('change', this.emp_code);
      this.$emit('changed',this.emp_code);
    },
    clear() {
      this.inputStyle=false;
      this.$emit('update:value', null);
    },
    async findOptions(name){
      this.inputStyle=false;
      if(name == null || name == ""){
        this.ei_list = [];
        return;
      }
      this.ei_loading=true;
      this.ei_list = await elm.post('/business/my/GetEIData', {name:name });
      if(this.ei_list.length==0 && this.inputAdd==true)
      {
        this.inputStyle=true;
        this.ei_list.push({id:name,name:name});
      }
      this.ei_loading=false;
    }
  }
}
</script>